package com.tianji.config;

import com.tianji.advisor.RecordOptimizationAdvisor;
import com.tianji.memory.OptimizeChatMemoryRepository;
import com.tianji.memory.mysql.JdbcChatMemoryRepository;
import com.tianji.memory.mongo.MongoDBChatMemoryRepository;
import com.tianji.memory.redis.RedisChatMemoryRepository;
import com.tianji.tools.CourseTools;
import com.tianji.tools.OrderTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.elasticsearch.ElasticsearchVectorStore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringAIConfig {

    @Value("${tj.ai.memory.max:100}")
    private Integer maxMessages;

    /**
     * 配置 ChatClient
     */
    @Bean
    public ChatClient chatClient(ChatClient.Builder chatClientBuilder,
                                 Advisor loggerAdvisor, // 日志记录器
                                 Advisor messageChatMemoryAdvisor, // 会话记录
                                 RecordOptimizationAdvisor recordOptimizationAdvisor
    ) {
        return chatClientBuilder
                .defaultAdvisors(loggerAdvisor,messageChatMemoryAdvisor,recordOptimizationAdvisor)
                .build();
    }

    /**
     * 优化对话历史记录
     */
    @Bean
    public RecordOptimizationAdvisor recordOptimizationAdvisor(OptimizeChatMemoryRepository repository) {
        return new RecordOptimizationAdvisor(repository);
    }

    @Bean
    public Advisor messageChatMemoryAdvisor(ChatMemory chatMemory) {
        return MessageChatMemoryAdvisor.builder(chatMemory).build();
    }

    /**
     * 配置 QuestionAnswerAdvisor 向量数据库  想实现对向量库的权限控制 需要重写VectorStore 接口
     * @param vectorStore
     * @return
     */
    @Bean
    public QuestionAnswerAdvisor questionAnswerAdvisor(VectorStore vectorStore) {
        return QuestionAnswerAdvisor.builder(vectorStore)
                .searchRequest(SearchRequest.builder()
                        .similarityThreshold(0.6d)
                        .topK(6)
                        .build())
                .build();
    }



    @Bean
    public ChatClient summaryClient(ChatClient.Builder chatClientBuilder,
                                    Advisor loggerAdvisor) {
        return chatClientBuilder
                .defaultAdvisors(loggerAdvisor) //添加 Advisor 功能增强
                .build();
    }


    @Bean
    @ConditionalOnProperty(name = "tj.ai.memory.type", havingValue = "redis", matchIfMissing = true)
    public ChatMemoryRepository redisChatMemoryRepository() {
        return new RedisChatMemoryRepository();
    }

    // 添加新方法支持 数据库存储对话记录 在tj.ai.memory.type=mysql时生效
    @Bean
    @ConditionalOnProperty(value = "tj.ai.memory.type",havingValue = "mysql")
    public ChatMemoryRepository jdbcChatMemoryRepository() {
        return new JdbcChatMemoryRepository();
    }

    // 新增方法支持 mongo存储对话记录 当tj.ai.memory.type=mongodb时生效
    @Bean
    @ConditionalOnProperty(value = "tj.ai.memory.type",havingValue = "mongodb")
    public ChatMemoryRepository mongoChatMemoryRepository() {
        return new MongoDBChatMemoryRepository();
    }

    @Bean
    public ChatMemory chatMemory(ChatMemoryRepository chatMemoryRepository) {
        return MessageWindowChatMemory.builder()
                .chatMemoryRepository(chatMemoryRepository)
                .maxMessages(maxMessages)
                .build();
    }

    /**
     * 日志记录器
     */
    @Bean
    public Advisor loggerAdvisor() {
        return new SimpleLoggerAdvisor();
    }
}